package com.webchat.chat.web;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.SensitiveWord;
import com.webchat.chat.service.SensitiveWordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by admin on 2018-07-03.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/sensitive/word")
public class SensitiveWordController extends BaseController {
    @Resource
    private SensitiveWordService sensitiveWordService;

    @PostMapping("/add")
    public Result add(SensitiveWord sensitiveWord) {
        sensitiveWord.setTenantCode(ShiroUtil.getTenantCode());
        sensitiveWordService.saveSensitiveWord(sensitiveWord);

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        sensitiveWordService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SensitiveWord sensitiveWord) {
        sensitiveWordService.update(sensitiveWord);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        SensitiveWord sensitiveWord = sensitiveWordService.findById(id);
        return ResultGenerator.genSuccessResult(sensitiveWord);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SensitiveWord> list = sensitiveWordService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
